<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    #result {
      width: 200px;
      height: 100px;
      border: 1px solid #90b
    }
  </style>
</head>
<body>
  <div id="result"></div>



  <script>
    const result = document.getElementById('result')
    // 绑定键盘按下事件
    window.onkeydown = function () {
      const xhr = new XMLHttpRequest()
      xhr.responseType = 'json'
      xhr.open('GET', 'http://127.0.0.1:8000/json-server')

      xhr.send()

      xhr.onreadystatechange = function () {
        if(xhr.readyState === 4) {
          if(xhr.status >=200 && xhr.status < 300) {
            // console.log(xhr.response)
            // result.innerHTML = xhr.response

            // 手动转换数据
            // let data = JSON.parse(xhr.response)
            // result.innerHTML = data.name

            // 自动转换
            console.log(xhr.response) //  xhr.responseType = 'json' 转换后的json数据 无需再手动JSON.parse
            result.innerHTML = xhr.response.name
          }
        }
      }
    }
  </script>







</body>
</html>